(async function () {
    let re = await getNewData()
    for (let i = 0; i < re.length; i++) {
        let id = re[i].data.id
        let sum = re[i].data.sum
        let goal = re[i].data.goal
        console.log(id, sum, goal)
        updatePage(id, sum, goal)
    }
})()

function getNewData() {
    let projects = document.getElementsByClassName("charity_project")
    let reqList = []
    let resList = []
    for (let i = 0; i < projects.length; i++) {
        let req = axios.get("http://localhost:9000/askDonate" + "?" + "id=" + i)
        reqList.push(req)
    }
    return axios.all(reqList).then(axios.spread(function (...resList) {
        return resList
    }))
}

function updatePage(id, sum, goal) {
    $("#" + id).find(".money").eq(0)[0].innerHTML = "￥" + goal
    $("#" + id).find(".money").eq(1)[0].innerHTML = "￥" + sum
    let point
    if (sum == 0)
        point = 0
    else if (sum == goal) {
        $("#" + id).find(".do_status").eq(0)[0].innerHTML = "已完成"
        $("#" + id).find(".c-circle").eq(0).css("background-color", "grey")
        $("#" + id).find(".c-circle").eq(0).css("border-color", "darkgrey")
        point = 100
    }
    else {
        point = sum * 100 / goal
    }
    $("#" + id).find(".c-circle").eq(0)[0].innerHTML = point + "%"
}